<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/8/3
 * Time: 11:21
 */

namespace Api\Service;


class SheetRecordService
{
    /**
     * 保存录音
     * @param $param
     * @return mixed
     */
    public function save($param)
    {
        $model = M('SheetRecord');
        $res = $model->create($param);
        if ($res) {
            $param['user_id'] = $param['uid'];
            $param['create_time'] = date('Y-m-d H:i:s', time());
            $id = $model->add($param);

            //曲谱录音数+1
            M('Sheet')->where(array('id'=>$param['sheet_id']))->setInc('record_count');

            return $id;
        } else {
            E($model->getError());
        }
    }

    /**
     * 通过参数获取录音url
     * @param $param
     * @return array
     */
    public function getUrlByParam($param)
    {
        if ($param['sheet_id']) {
            $map = array(
                'sheet_id' => $param['sheet_id'],
                'is_delete' => 0
            );
        } else {
            $map = array(
                'user_id' => $param['uid'],
                'is_delete' => 0
            );
        }
        $model = M('SheetRecord');

        $count = $model->where($map)->count();
        $listRows = empty( $param['listRows'] ) ? 10 : $param['listRows'];
        $page = new PageService( $param, $count, $listRows );
        $list = $model->where($map)->order( 'create_time desc' )->limit( $page->firstRow . ',' . $page->listRows )->select();

        if ($param['sheet_id']) {
            //获取用户信息
            $userIds = array_column( $list, 'user_id' );
            $userService = new UserService();
            $info = $userService->getMapByUids( $userIds );
        } else {
            //获取谱单信息
            $sheetIds = array_column($list, 'sheet_id');
            $sheetService = new SheetService();
            $info = $sheetService->getMapBySheetIds($sheetIds);
        }

        $recordList = array();
        foreach ($list as $key => $val) {
            $recordList['list'][$key]['id'] = $val['id'];
            $recordList['list'][$key]['sheet'] = $param['sheet_id'] ? $val['sheet_id'] : $info[$val['sheet_id']];
            $param['sheet_id'] ? $recordList['list'][$key]['user'] =  $info[$val['user_id']] : $recordList['list'][$key]['user_id'] = $val['user_id'];
            $recordList['list'][$key]['url'] = $val['url'];
            $recordList['list'][$key]['duration'] = $val['duration'];
            $recordList['list'][$key]['create_time'] = $val['create_time'];

        }
        if (!empty($recordList['list'])) {
            $recordList['nowPage'] = $page->nowPage;
            $recordList['totalPage'] = ceil( $count / $listRows );
            $recordList['totalCount'] = $count;
        } else {
            $recordList['list'] = array();
        }
        return $recordList;
    }


    /**
     * 删除录音
     */
    public function del($param)
    {
        $model = M('SheetRecord');
        $map = array('id'=>$param['record_id']);
        $ids = $model->where($map)->field('user_id, sheet_id')->select();
        $userId = $ids[0]['user_id'];
        $sheetId = $ids[0]['sheet_id'];
        if($param['uid'] != $userId) {
            return false;
        }

        $data = array(
            'is_delete' => 1,
            'update_time' => date('Y-m-d H:i:s', time())
        );

        $res = $model->where($map)->save($data);
        //曲谱录音数-1
        M('Sheet')->where(array('id'=>$sheetId))->setDec('record_count');

        return $res;
    }

    /**
     * 获取录音的基本信息
     * @param $list
     * @return array
     */
    public function recordInfo($list)
    {
        $recordIds = array_column($list, 'record_id');
        $recordList = M('SheetRecord')->where(array('id'=> array('in', $recordIds)))->select();
        //获取用户信息
        $userIds = array_unique(array_column( $recordList, 'user_id' ));
        $userService = new UserService();
        $userInfo = $userService->getMapByUids( $userIds );
        //获取谱单信息
        $sheetIds = array_unique(array_column($recordList, 'sheet_id'));
        $sheetService = new SheetService();
        $sheetInfo = $sheetService->getMapBySheetIds($sheetIds);
        $recordMap = array();
        foreach ($recordList as $record) {
            $recordMap[$record['id']]['user'] = $userInfo[$record['user_id']];
            $recordMap[$record['id']]['sheet'] = $sheetInfo[$record['sheet_id']];
            $recordMap[$record['id']]['duration'] = $record['duration'];
            $recordMap[$record['id']]['url'] = $record['url'];
        }
        return $recordMap;
    }
}